package com.audaque.springboot.foshanupload.web.webdemo.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @author zgb
 * @desc ...
 * @date 2023-08-03 20:05:39
 */
@Slf4j
@RestController
@RequestMapping("atomicReference")
public class AtomicReferenceController {
    @GetMapping("test")
    public void test() {
        final String[] message = {"hello"};
        AtomicReference<String> atomicReference = new AtomicReference<String>(message[0]);
        new Thread("Thread 1") {
            public void run() {
                //线程安全
                atomicReference.compareAndSet(message[0], "Thread 1");
                //非线程安全
                message[0] = message[0].concat("-Thread 1!");
            }

            ;
        }.start();
        //非原子
        System.out.println("Message is: " + message[0]);
        //原子
        System.out.println("Atomic Reference of Message is: " + atomicReference.get());

    }

}
